/**
 *  Copyright 23.07.2012 oliver.keusemann@googlemail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package de.xelerity.geolessi.srv.impl.poi.meinestadt;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import de.xelerity.geolessi.preferences.Preferences;
import de.xelerity.geolessi.srv.ServiceProvider;
import de.xelerity.geolessi.srv.mixed.URLGenerator;
import de.xelerity.geolessi.srv.mixed.WSCallerI;
import de.xelerity.geolessi.srv.poi.HalteStellenAdresseGetterI;

public class HalteStellenAdresseGetterImpl implements HalteStellenAdresseGetterI {

	public String getAddressInfo(String pHalteStelleID) {
		if (pHalteStelleID == null || pHalteStelleID.trim().length() <= 0) {
			return "";
		}

		try {

			String URL = setUpUrl(pHalteStelleID);
			WSCallerI wsCaller2 = ServiceProvider.getWSCaller();

			if (Preferences.useProxy) {
				wsCaller2.addProperty("PROXYHOST", Preferences.Proxy);
				wsCaller2.addProperty("PROXYPORT", Preferences.ProxyPort + "");
			}

			byte[] callResB = wsCaller2.call(URL);

			String callRes = new String(callResB, "UTF-8");

			// System.out.println(callRes);

			String ResTitle = "";
			try {
				Pattern regex = Pattern.compile("\"title\"\\s*:\\s*\"(.+?)\"[,}\\]]", Pattern.CASE_INSENSITIVE);
				Matcher regexMatcher = regex.matcher(callRes);
				if (regexMatcher.find()) {
					ResTitle = regexMatcher.group(1);
				}
			} catch (PatternSyntaxException ex) {
				// Syntax error in the regular expression
			}

			String ResZip = "";
			try {
				Pattern regex = Pattern.compile("\"zip\"\\s*:\\s*\"(.+?)\"[,}\\]]", Pattern.CASE_INSENSITIVE);
				Matcher regexMatcher = regex.matcher(callRes);
				if (regexMatcher.find()) {
					ResZip = regexMatcher.group(1);
				}
			} catch (PatternSyntaxException ex) {
				// Syntax error in the regular expression
			}

			String ResStreet = "";
			try {
				Pattern regex = Pattern.compile("\"street\"\\s*:\\s*\"(.+?)\"[,}\\]]", Pattern.CASE_INSENSITIVE);
				Matcher regexMatcher = regex.matcher(callRes);
				if (regexMatcher.find()) {
					ResStreet = regexMatcher.group(1);
				}
			} catch (PatternSyntaxException ex) {
				// Syntax error in the regular expression
			}

			String ResCity = "";
			try {
				Pattern regex = Pattern.compile("\"city\"\\s*:\\s*\"(.+?)\"[,}\\]]", Pattern.CASE_INSENSITIVE);
				Matcher regexMatcher = regex.matcher(callRes);
				if (regexMatcher.find()) {
					ResCity = regexMatcher.group(1);
				}
			} catch (PatternSyntaxException ex) {
				// Syntax error in the regular expression
			}

			String DBMoreInfoUrl = "";
			try {
				Pattern regex = Pattern.compile("\"moreUrl1\"\\s*:\\s*\"(.+?)\"[,}\\]]", Pattern.CASE_INSENSITIVE);
				Matcher regexMatcher = regex.matcher(callRes);
				if (regexMatcher.find()) {
					DBMoreInfoUrl = regexMatcher.group(1);
					// Hole si parameter
					String BahnHSID = URLGenerator.getParameter(DBMoreInfoUrl, "si");
					MeineStadtHSID2BahnHSID.addMapping(pHalteStelleID, BahnHSID);
				}
			} catch (PatternSyntaxException ex) {
				// Syntax error in the regular expression
			}

			if (ResStreet == null || ResStreet.trim().length() <= 0) {
				ResStreet = ResTitle;
			}

			String RES = ResStreet.trim() + ", " + ResZip.trim() + " " + ResCity.trim();

			RES = RES.replaceAll("\\s\\s+", " ");
			RES = RES.replaceAll(",\\s*,+", ",");

			String A = RES.replaceAll("[, ]", "");
			if (A.length() <= 0) {
				return "";
			}

			return RES;

		} catch (Throwable e) {
			return "";
		}
	}

	private String setUpUrl(String ID) throws Exception {
		if (ID == null) {
			throw new IllegalArgumentException("ID nicht (korrekt) gesetzt");
		}

		// http://stadtplan.meinestadt.de/xml/java-service/poi?poi=1900935585&output=json&context=poi-service
		// http://mobile.bahn.de/bin/mobil/bhftafel.exe/dox?si=559440&bt=dep&ti=actual&p=1111111111&max=10&mode=actual&start=yes

		ID = ID.replaceAll("\\s", "");

		URLGenerator urlGenerator = new URLGenerator("stadtplan.meinestadt.de", "xml/java-service/poi");

		urlGenerator.addParameter("output", "json");
		urlGenerator.addParameter("context", "poi-service");
		urlGenerator.addParameter("poi", ID);

		return urlGenerator.getURL();

	}

	public static void main(String[] args) {

		System.out.println(new HalteStellenAdresseGetterImpl().getAddressInfo("1900935585"));
	}

}
